﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace _0712518_0712510_LTGame
{
    public class MapManager
    {
        public Character Zero;
        public Map map;

        public MapManager()
        {
            Zero = new Hero();
            map = new Map();
        }
        public void LoadContent(ContentManager Content)
        {
            Zero.LoadContent(Content);
            map.LoadContent(Content);
        }
        public void checkToMap()
        {
            int i, x1, x2, y1, y2;
            Vector2 pos1 = new Vector2();
            /* Remove the user from the ground */

            // onGround = 0;
            pos1.X = Zero.position.X + Zero.currentState.conclision.X;
            if (Zero.faceDirection == 0)
            {
                pos1.X = Zero.position.X - (Zero.currentState.conclisionSize.X + Zero.currentState.conclision.X);
            }
            pos1.Y = Zero.position.Y + Zero.currentState.conclision.Y;
            /* Test the horizontal movement first */

            i = Zero.currentState.conclisionSize.Y > map.tile_size ? map.tile_size : Zero.currentState.conclisionSize.Y;

            for (; ; )
            {

                x1 = (int)(pos1.X + Zero.pos2.X) / map.tile_size;
                x2 = (int)(pos1.X + Zero.pos2.X + Zero.currentState.conclisionSize.X - 1) / map.tile_size;


                y1 = (int)(pos1.Y) / map.tile_size;
                y2 = (int)(pos1.Y + i - 1) / map.tile_size;

                if (x1 >= 0 && x2 < 37 && y1 >= 0 && y2 < 14)
                {
                    if (Zero.pos2.X > 0)
                    {
                        /* Trying to move right */

                        if ((map.tile[y1, x2] != 0) || (map.tile[y2, x2] != 0))
                        {
                            /* Place the player as close to the solid tile as possible */

                            pos1.X = x2 * map.tile_size;

                            pos1.X -= Zero.currentState.conclisionSize.X; //+ 1;

                            Zero.pos2.X = 0;
                        }
                    }

                    else if (Zero.pos2.X < 0)
                    {
                        /* Trying to move left */

                        if ((map.tile[y1, x1] != 0) || (map.tile[y2, x1] != 0))
                        {
                            /* Place the player as close to the solid tile as possible */

                            pos1.X = (x1 + 1) * map.tile_size; //+1;

                            Zero.pos2.X = 0;
                        }
                    }
                }

                if (i == Zero.currentState.conclisionSize.Y)
                {
                    break;
                }

                i += map.tile_size;

                if (i > Zero.currentState.conclisionSize.Y)
                {
                    i = Zero.currentState.conclisionSize.Y;
                }
            }

            /* Now test the vertical movement */

            i = Zero.currentState.conclisionSize.X > map.tile_size ? map.tile_size : Zero.currentState.conclisionSize.X;

            for (; ; )
            {

                x1 = (int)(pos1.X) / map.tile_size;
                x2 = (int)(pos1.X + i - 1) / map.tile_size;

                y1 = (int)(pos1.Y + Zero.pos2.Y) / map.tile_size;
                y2 = (int)(pos1.Y + Zero.pos2.Y + Zero.currentState.conclisionSize.Y) / map.tile_size;

                if (x1 >= 0 && x2 < 37 && y1 >= 0 && y2 < 14)
                {
                    if (Zero.pos2.Y > 0)
                    {
                        /* Trying to move down */
                        Zero.onGround = 0;
                        if ((map.tile[y2, x1] != 0) || (map.tile[y2, x2] != 0))
                        {
                            /* Place the player as close to the solid tile as possible */

                            pos1.Y = y2 * map.tile_size;
                            pos1.Y -= Zero.currentState.conclisionSize.Y;
                           
                            Zero.pos2.Y = 0;
                          //  input = 0;
                            Zero.status = 0;
                            Zero.onGround = 1;
                        }
                    }

                    else if (Zero.pos2.Y < 0)
                    {
                        /* Trying to move up */

                        if ((map.tile[y1, x1] != 0) || (map.tile[y1, x2] != 0))
                        {
                            /* Place the player as close to the solid tile as possible */

                            pos1.Y = (y1 + 1) * map.tile_size;
                            Zero.status = 0;
                            Zero.pos2.Y = 0;
                        }
                    }
                }

                if (i == Zero.currentState.conclisionSize.X)
                {
                    break;
                }

                i += map.tile_size;

                if (i > Zero.currentState.conclisionSize.X)
                {
                    i = Zero.currentState.conclisionSize.X;
                }
            }

            /* Now apply the movement */

            pos1.X += Zero.pos2.X;
            pos1.Y += Zero.pos2.Y;
            Zero.position.X = pos1.X - Zero.currentState.conclision.X;
            //if (Zero.faceDirection == 1)
            //{
            //    Zero.position.X = pos1.X - Zero.currentState.conclision.X;
            //}
            if (Zero.faceDirection == 0)
            {
                Zero.position.X = pos1.X + (Zero.currentState.conclisionSize.X + Zero.currentState.conclision.X);
            }
            Zero.position.Y = pos1.Y - Zero.currentState.conclision.Y;

            if (pos1.X < 0)
            {
                pos1.X = 0;
                Zero.position.X = pos1.X + (Zero.currentState.conclisionSize.X + Zero.currentState.conclision.X); //+ 1;
            }

            else if (pos1.X + Zero.currentState.conclisionSize.X >= map.MaxX)
            {
                pos1.X = map.MaxX - Zero.currentState.conclisionSize.X; //- 1;
                Zero.position.X = pos1.X - Zero.currentState.conclision.X;
            }

            if (pos1.Y > map.MaxY)
            {
                pos1.Y = pos1.X = Zero.pos2.X = Zero.pos2.Y = 0;
                Zero.position.X = pos1.X - Zero.currentState.conclision.X;
                Zero.position.Y = pos1.Y - Zero.currentState.conclision.Y;
            }
        }
        public void Update(GameTime gameTime, int clientWidth, int clientHeight)
        {
            Zero.Update(gameTime);
            checkToMap();
            map.centerEntityOnMap(Zero, clientWidth, clientHeight);
        }

        public void Draw(SpriteBatch spriteBatch, int clientWidth, int clientHeight)
        {
            if (Zero.faceDirection == 1)
            {
                //    spriteBatch.Draw(m_Texture2D, new Vector2(pos1.X - m_Map1.StartPosition.X, pos1.Y - m_Map1.StartPosition.Y),
                //new Rectangle((currentFrame.X * frameSize.X) + currentFrame.X, 1, frameSize.X, frameSize.Y),
                //Color.White, 0, Vector2.Zero,
                //1, SpriteEffects.None, 0);
                spriteBatch.Draw(Zero.movement[Zero.currentMove].m_Texture2D, new Vector2((Zero.position.X + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.X) - map.StartPosition.X, (Zero.position.Y + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.Y) - map.StartPosition.Y),
            new Rectangle((int)(Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].start_position.X + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.X), (int)(Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].start_position.Y + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.Y), Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].size.X, Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].size.Y),
            Color.White, 0, Vector2.Zero,
            1, SpriteEffects.None, 0);
            }
            if (Zero.faceDirection == 0)
            {
                //    spriteBatch.Draw(m_Texture2D, new Vector2(pos1.X - m_Map1.StartPosition.X, pos1.Y - m_Map1.StartPosition.Y),
                //new Rectangle((currentFrame.X * frameSize.X) + currentFrame.X, 1, frameSize.X, frameSize.Y),
                //Color.White, 0, Vector2.Zero,
                //1, SpriteEffects.FlipHorizontally, 0);
                spriteBatch.Draw(Zero.movement[Zero.currentMove].m_Texture2D, new Vector2((Zero.position.X - (Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].size.X + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.X)) - map.StartPosition.X, (Zero.position.Y + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.Y) - map.StartPosition.Y),
            new Rectangle((int)(Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].start_position.X + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.X), (int)(Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].start_position.Y + Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].position2draw.Y), Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].size.X, Zero.movement[Zero.currentMove].skill[Zero.movement[Zero.currentMove].currentFrame].size.Y),
            Color.White, 0, Vector2.Zero,
            1, SpriteEffects.FlipHorizontally, 0);
            }
            map.Draw(spriteBatch, clientWidth, clientHeight);
            
        }
    }
}
